<?php
/**
 * dar字段
 *
 * @package util.dar
 * @version $Id: IDarField.php 312 2008-04-23 16:29:38Z iwind $
 * @since 0.0.3
 */
class IDarField extends IObject {
	private $name;
	private $type;
	private $column;
	private $maxLength = 0;
	private $notNull = false;
	private $primaryKey = false;
	private $autoIncrement = false;
	private $binary = false;
	private $unsigned = false;
	private $hasDefault = false;
	private $defaultValue;
	private $relation = array();
	private $tableRef;
	private $isVirtual = false;
	private $expression = null;
	
	/**
	 * 构造
	 *
	 * @since 0.0.3
	 */
	public function __construct() {

	}

	/**
	 * 设置名称
	 *
	 * @param string $name 名称
	 * @since 0.0.3
	 */
	public function setName($name) {
		$this->name = $name;
	}
	
	/**
	 * 取得名称
	 *
	 * @return string
	 * @since 0.0.3
	 */
	public function getName() {
		if ($this->name === null) {
			return native::nameToJava($this->column);//需要配置替换
		}
		return $this->name;
	}
	
	/**
	 * 设置字段类型
	 *
	 * @param string $type 类型
	 * @since 0.0.3
	 */
	public function setType($type) {
		$this->type = $type;
	}
	
	/**
	 * 取得字段类型
	 *
	 * @return string
	 * @since 0.0.3
	 */
	public function getType() {
		return $this->type;
	}
	
	/**
	 * 设置字段列名
	 *
	 * @param string $column 列名
	 * @since 0.0.3
	 */
	public function setColumn($column) {
		$this->column = $column;
	}
	
	/**
	 * Enter description here...
	 *
	 * @return unknown
	 * @since 0.0.3
	 */
	public function getColumn() {
		if ($this->column === null) {
			return native::nameToC($this->name);
		}
		return $this->column;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $maxLength
	 * @since 0.0.3
	 */
	public function setMaxLength($maxLength) {
		$this->maxLength = $maxLength;
	}
	
	/**
	 * Enter description here...
	 *
	 * @return unknown
	 * @since 0.0.3
	 */
	public function getMaxLength() {
		return $this->maxLength;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $notNull
	 * @since 0.0.3
	 */
	public function setNotNull($notNull) {
		$this->notNull = $notNull;
	}
	
	/**
	 * Enter description here...
	 *
	 * @return unknown
	 * @since 0.0.3
	 */
	public function isNotNull() {
		return $this->notNull;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $primaryKey
	 * @since 0.0.3
	 */
	public function setPrimaryKey($primaryKey) {
		$this->primaryKey = $primaryKey;
	}
	
	/**
	 * Enter description here...
	 *
	 * @return unknown
	 * @since 0.0.3
	 */
	public function isPrimaryKey() {
		return $this->primaryKey;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $autoIncrement
	 * @since 0.0.3
	 */
	public function setAutoIncrement($autoIncrement) {
		$this->autoIncrement = $autoIncrement;
	}
	
	/**
	 * Enter description here...
	 *
	 * @return unknown
	 * @since 0.0.3
	 */
	public function isAutoIncrement() {
		return $this->autoIncrement;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $binary
	 * @since 0.0.3
	 */
	public function setBinary($binary) {
		$this->binary = $binary;
	}
	
	/**
	 * Enter description here...
	 *
	 * @return unknown
	 * @since 0.0.3
	 */
	public function isBinary() {
		return $this->binary;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $unsigned
	 * @since 0.0.3
	 */
	public function setUnsigned($unsigned) {
		$this->unsigned = $unsigned;
	}
	
	/**
	 * Enter description here...
	 *
	 * @return unknown
	 * @since 0.0.3
	 */
	public function isUnSigned() {
		return $this->unsigned;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $hasDefault
	 * @since 0.0.3
	 */
	public function setHasDefault($hasDefault) {
		$this->hasDefault = $hasDefault;
	}
	
	/**
	 * Enter description here...
	 *
	 * @return unknown
	 * @since 0.0.3
	 */
	public function hasDefault() {
		return $this->hasDefault;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $defaultValue
	 * @since 0.0.3
	 */
	public function setDefaultValue($defaultValue) {
		$this->defaultValue = $defaultValue;
	}
	
	/**
	 * Enter description here...
	 *
	 * @return unknown
	 * @since 0.0.3
	 */
	public function getDefaultValue() {
		return $this->defaultValue;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $value
	 * @since 0.0.3
	 * @throws IDarException 有异常时抛出
	 */
	public function filterValue(&$value) {
		if ($value === null && $this->hasDefault) {
			$value = $this->getDefaultValue();
		}
		
		if ($this->maxLength > 0) {
			//$value = substr($value, 0, $this->maxLength);
			if (strlen($value) > $this->maxLength) {
				throw new IDarException("model '" . $this->table->getModel()->getName() . "' table '" . $this->table->getName() . "': field value is too long, max length of the field '" . $this->getName() . "' is {$this->maxLength}");
			}
		}
		
		if ($this->notNull && $value === null) {
			throw new IDarException("field '" . $this->getName() . "' value can't be null");
		}
		
		if ($this->unsigned && is_numeric($value)) {
			$value = abs($value);
		}
	}
	
	/**
	 * 设置关联模型
	 *
	 * @param array $relation 关联
	 * @since 0.1
	 */
	public function setRelation(array $relation = null) {
		if ($relation != null) {
			$this->relation = $relation;
			$this->isVirtual = true;
		}
	}
	
	/**
	 * 取得关联
	 *
	 * @return IDarRelation
	 * @since 0.1
	 */
	public function getRelation() {
		if ($this->relation != null) {
			return IDarRelation::create($this, $this->relation);
		}
	}
	
	/**
	 * 设置包含该字段的表
	 *
	 * @param IDarTable $table dar表
	 * @since 0.1
	 */
	public function setTable(IDarTable $table) {
		$this->tableRef = IObjectFactory::addRef($table);
	}
	
	/**
	 * 取得包含该字段的表
	 *
	 * @return IDarTable
	 * @since 0.1
	 */
	public function getTable() {
		return IObjectFactory::getRef($this->tableRef);
	}
	
	/**
	 * 取得虚拟属性
	 *
	 * @return boolean
	 * @since 0.1
	 */
	public function isVirtual() {
		return $this->isVirtual;
	}
	
	/**
	 * 设置虚拟属性
	 *
	 * @param boolean $isVirtual 是否虚拟
	 * @since 0.1
	 */
	public function setVirtual($isVirtual) {
		$this->isVirtual = $isVirtual;
	}
	
	/**
	 * 设置表达式
	 *
	 * @param string $expression 表达式
	 * @since 0.2
	 */
	public function setExpression($expression) {
		$this->expression = $expression;
	}
	
	/**
	 * 取得表达式
	 *
	 * @return string
	 * @since 0.2
	 */
	public function getExpression() {
		return $this->expression;
	}
}

?>